#include "StdAfx.h"
#include "PlayVoiceNode.h"
#include "TinyServerDefine.h"
#include "TalkFlow.h"
#include <io.h>

namespace IVRFlow{
CPlayVoiceNode::CPlayVoiceNode(void)
{
}

CPlayVoiceNode::~CPlayVoiceNode(void)
{
}

int CPlayVoiceNode::Enter(void)
{
	//execute js script here
	CTalkFlow* flow = g_FlowPool.getFlowByTalkID(m_talkID);
	if(flow != NULL)
		flow->ExecuteJScript(m_jsScript);
	//send playvoice command
	play();
	while(m_blockMode)
	{
		Sleep(200);
	}
	//pause the flow is needed.
	return 0;
}

/*bool CPlayVoiceNode::playVoiceFile(std::string path)
{
	return false;
}*/

void CPlayVoiceNode::setVoiceFile(std::string path)
{
	return;
}

void CPlayVoiceNode::play()
{
	CTalkFlow* flow = g_FlowPool.getFlowByTalkID(m_talkID);
	if(flow != NULL)
	{
		
		if(_access(m_voiceFile.c_str(),0) >0)
		{
			HAL::CAnalogTrunk* device = g_DevicePool.GetTrunkByID(flow->CalledID);
			if(device->playVoice(m_voiceFile) == false)
				flow->FlowExceptioned(0,"Play Voice Failed!");
		}
	}
	return;
}
//stop playing file

void CPlayVoiceNode::stop()
{
	return;
}

void CPlayVoiceNode::processKeyRecived(std::string key)
{
	CTalkFlow* flow = g_FlowPool.getFlowByTalkID(m_talkID);
	if(flow != NULL)
		flow->ExecuteJScript(m_keyJScript);
	
	m_blockMode = false;
}
void CPlayVoiceNode::processPlayFinished()
{
	CTalkFlow* flow = g_FlowPool.getFlowByTalkID(m_talkID);
	if(flow != NULL)
		flow->ExecuteJScript(m_playFinishedJScript);

	m_blockMode = false;
}

}
